package com.renting.modules.sys.web;

import com.renting.common.entity.Result;
import com.renting.common.entity.ResultList;
import com.renting.modules.sys.dao.UserDao;
import com.renting.modules.sys.entity.User;
import com.renting.modules.sys.service.UserService;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.*;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/4/10.
 */
@Controller
public class UserExportController {

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "v1/sys/users/excel", method = RequestMethod.GET)
    @CrossOrigin("*")
    public ResponseEntity<byte[]> excelExport() {
        try {
            List<User> userList = userService.findAll();
            ResultList result = new ResultList();
            result.setData(userList);
            Template template = freeMarkerConfig.getConfiguration().getTemplate("user_export.ftl");
            String docContent = FreeMarkerTemplateUtils.processTemplateIntoString(template, result);
            HttpHeaders headers = new HttpHeaders();
            String fileName = null;
            fileName = new String("用户导出记录.xls".getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("", fileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(docContent.getBytes("utf-8"), headers, HttpStatus.CREATED);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping(value = "v1/sys/users/template",method = RequestMethod.GET)
    @CrossOrigin("*")
    public ResponseEntity<byte[]> excel(){
        try {
            File template = ResourceUtils.getFile("classpath:templates/user_export.xls");
            String fileName = new String("用户信息导入模板.xls".getBytes("UTF-8"),"iso-8859-1");
            byte[] buffer = null;
            FileInputStream fis = new FileInputStream(template);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
            HttpHeaders headers = new HttpHeaders();

            headers.setContentDispositionFormData("attachment", fileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(buffer, headers, HttpStatus.CREATED);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
